﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DSS_XNA.Engine.Error;
using DSS_XNA.Engine.HUD.Controls;
using System.Reflection;
using DSS_XNA.Engine.Script;

namespace DSS_XNA.Engine.Script.Commands
{
    public class ScriptCommand_AddHUDControl
    {
        #region Static Methods

        public static void Action(Dictionary<string, string> args, ScriptManager manager, Game1 engine)
        {
            if (!args.ContainsKey("type"))
                throw new DSSException("No Type parameter for AddHUDControl");

            string className = manager.GetParameterValue(args["type"]).ToString();

            Type type = Type.GetType(className);
            if (type == null)
                throw new DSSException(String.Format("Failed to load actor type: {0}", className));
            if (type.GetInterface("IHUDControl") != typeof(IHUDControl))
                throw new DSSException(String.Format("Type: {0} does not inherit from IHUDControl", className));

            ConstructorInfo con = type.GetConstructor(new Type[] { });

            IHUDControl control = con.Invoke(null) as IHUDControl;
            if (control == null)
                throw new DSSException("'control' is null.");
            engine.Hud.Controls.Add(control);
        }

        #endregion
    }
}
